package MyLinkst;

public class MyLinkList {

    static  class ListNode{
        public  int val;
        public ListNode prev;
        public  ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public  ListNode head;
    public  ListNode last;

    /*public MyLinkList() {
        head =last =new ListNode(-1); //带头结点的表示
    }*/

    public void display() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println("");
    }
    public void addFirst(int data){
        ListNode node =new ListNode(data);
        if(head==null){
            head=node;
            last=node;
        }
        else {
            node.next=head;
            head.prev=node;
            head=node;
        }
    }
    public  void addLast(int data){
        ListNode node =new ListNode(data);
        if(last==null){
            head=node;
            last=node;
        }
        else {
            node.prev=last;
            last.next=node;
            last=node;
        }
    }
    public void addIndex(int index,int data){
        check(index);
        ListNode cur=search(index);
        ListNode node=new ListNode(data);

        if(index==0){
            addFirst(data);
            return;
        }
        node.next=cur;
        node.prev=cur.prev;
        cur.prev.next=node;
        cur.prev=node;

    }
    public ListNode search(int index){
        ListNode cur=head;
        while (index!=0){
            cur=cur.next;
            index--;
        }
        return cur;
    }
    public  boolean check(int index){
        if(index<0||index>szie()){
            return  false;
        }
        return  true;
    }
    public  int szie(){
        ListNode cur =head;
        int count=0;
        while (cur!=null){
            cur=  cur.next;
            count++;
        }
        return  count;
    }
    public void remove(int data){
        ListNode cur=head;
        while (cur!=null){
            if(cur.val==data){
                if(cur==head){
                    if(head!=null){
                        head=head.next;
                        head.prev=null;
                    }
                    else {
                        head=null;
                    }
                }else if(cur==last){
                    last.prev.next=null;
                    last=last.prev;
                }else {
                    cur.prev.next=cur.next;
                    cur.next.prev=cur.prev;
                }
                return;
            }
            else {
                cur=cur.next;
            }
        }
    }
    public void removeAll(int data){
        ListNode cur=head;
        while (cur!=null){
            if(cur.val==data){
                if(cur==head){
                    if(head!=null){
                        head=head.next;
                        head.prev=null;
                    }
                    else {
                        head=null;
                    }
                }else if(cur==last){
                    last.prev.next=null;
                    last=last.prev;
                }else {
                    cur.prev.next=cur.next;
                    cur.next.prev=cur.prev;
                }
                cur=cur.next;
            }
            else {
                cur=cur.next;
            }
        }
    }
    public void clear(){
        ListNode cur =head;
        ListNode curNext =cur.next;
        while (cur!=null){
            cur.prev=null;
            cur.next=null;
            cur =curNext;
        }
        head =null;
        last=null;
    }
}
